package com.csg.boot.starter.builder;

import com.csg.boot.starter.constant.GrantEnum;
import com.csg.boot.starter.constant.SwaggerProperties;
import springfox.documentation.service.*;

/**
 * @author george
 */
public class GrantTypeBuilder implements BaseBuilder<GrantType, SwaggerProperties.GrantProperties> {

    private static GrantTypeBuilder builder = new GrantTypeBuilder();

    private GrantTypeBuilder() {
    }

    public static GrantTypeBuilder getInstance() {
        return builder;
    }

    @Override
    public GrantType build(SwaggerProperties.GrantProperties properties) {
        GrantEnum type = properties.getType();
        if (GrantEnum.AUTHORIZATION_CODE.equals(type)) {
            return new AuthorizationCodeGrant(
                    new TokenRequestEndpoint(properties.getTokenRequestUrl(), properties.getClientIdName(), properties.getClientSecretName()),
                    new TokenEndpoint(properties.getTokenEndUrl(), properties.getTokenName()));
        } else if (GrantEnum.APPLICATION.equals(type)) {
            return new ClientCredentialsGrant(properties.getTokenUrl());
        } else if (GrantEnum.IMPLICIT.equals(type)) {
            return new ImplicitGrant(new LoginEndpoint(properties.getLoginEndUrl()), properties.getTokenName());
        } else if (GrantEnum.PASSWORD.equals(type)) {
            return new ResourceOwnerPasswordCredentialsGrant(properties.getTokenUrl());
        }
        return new CredentialsGrant(type.toString(), properties.getTokenUrl());
    }

}
